from bs4 import BeautifulSoup
import lxml
import requests

def get_context(url):
	r = requests.get(url)
	soup = BeautifulSoup(r.text, 'lxml')
	soup.prettify()

	title = soup.title.string
	floors = soup.find_all(name='div', attrs={'class': 'd_post_content j_d_post_content'})
	users = soup.find_all(name='a', attrs={'class':'p_author_name j_user_card'})
	fl_con = [] # 每一楼的正文内容
	fl_user = [] # 每一楼的发言用户(hash)
	us_name = [] # 每一楼的发言用户(string)
	us_hash = [] # 本页面按照楼层排序的发言用户，按照在us_name里的顺序标记
	cnt = 0

	for f in floors:
		if not f.text == None:
			fl_con.append(f.text)

	
	for u in users:
		if not u.text == None:
			us_name.append(u.text)
			cnt += 1
		'''
		tmp = False
		count_us_name = 0
		for h in us_hash:
			if not u.text == None and u.text == h:
				tmp = True
				fl_user.append(count_us_name)
				break
			count_us_name += 1
		if not tmp:
			us_hash.append(u.text)
			fl_user.append(len(us_hash) - 1)
		'''

	return [fl_con, us_name]

def get_all_post(id):
	r = requests.get('https://tieba.baidu.com/p/' + str(id))
	soup = BeautifulSoup(r.text, 'lxml')
	soup.prettify()

	pages_root = soup.find(name='li', attrs={'class':'l_pager pager_theme_5 pb_list_pager'})
	pages = pages_root.find_all(name='a')
	last_id = 0

	# 获取这一个帖子内的最高页（写得太复杂了）
	if pages != []:
		last_1 = pages[len(pages) - 1]
		last_page = last_1.attrs['href']
		i = len(last_page) - 1
		stack = []
		while i >= 0:
			if not ('0' <= last_page[i] and last_page[i] <= '9'):
				break
			else:
				stack.append(int(last_page[i]))
				i -= 1
		stack.reverse()
		for s in stack:
			last_id *= 10
			last_id += s
	else:
		last_page = 'https://tieba.baidu.com/p/' + str(id) + '?pn=1'
		last_id = 1

	# 一一爬取所有页上的所有帖子
	leaf = 1
	main_fl_con = [] # 总的正文内容
	main_fl_user = [] # 每一楼发言用户(hash)
	main_us_name = [] # 每一楼发言用户(string)
	main_us_hash = [] # 记录所有有发言的用户
	while leaf <= last_id:
		returning = get_context('https://tieba.baidu.com/p/' + str(id) + '?pn=' + str(leaf))
		main_fl_con.extend(returning[0])
		main_us_name.extend(returning[1])
		tmp_fl_con = returning[0]
		tmp_us_name = returning[1]

		wh_con = len(main_fl_con) - len(tmp_fl_con) # 加入这一页之前的总楼数
		for name in tmp_us_name:
			wh_con += 1
			hash_name = 0 # main_us_hash计数器
			for h in main_us_hash:
				if h == name:
					main_fl_user.append(hash_name)
					break
				hash_name += 1
			if hash_name >= len(main_us_hash):
				main_us_hash.append(name)
				main_fl_user.append(len(main_us_hash) - 1)
			# print(hash_name)
		leaf += 1

	return [main_fl_con, main_fl_user]